package com.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.entity.Address;
import com.entity.Cake;
import com.entity.Consumer;
import com.entity.ShopCart;
import com.fasterxml.jackson.databind.Module;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.servers.AddressServers;
import com.servers.CakeServers;
import com.servers.ConsumerServers;
import com.servers.ShopCarService;

@Controller
public class OrderController {
	@Autowired
	private  ConsumerServers consumerserver;
	@Autowired
	private CakeServers cakeserver;
	@Autowired
	private AddressServers addressServers;
	@Autowired
	private ShopCarService shopCarService;
	
	//@ResponseBody
	//@RequestMapping("/shopcar")
	public String shopcar(String name,Model model) {
		//model.addAttribute("name", name+",");
		System.out.println("进入到添加购物车name:"+name);
		String carlist=name+",";
		Gson gson=new Gson();
		System.out.println("进入到添加购物车carlist:"+carlist);
		return gson.toJson(carlist);
	}
	

	@RequestMapping("/BookOrder")
	public ModelAndView goshopcar(HttpServletRequest request) {
		double sumPrice=0;
		ModelAndView mv=new ModelAndView();
		mv.setViewName("order");
		// Map<String,Object> map=new HashMap<>();		
		int cid=(int) request.getSession().getAttribute("CURR_CID");
		System.out.println("========用户ID"+cid);
		List<Address> list=addressServers.getAddressInfo(cid);
		for (Address address : list) {
			System.out.println("-----------address---");
			System.out.println(address);
		}
		Consumer consumer=consumerserver.getConsumerInfoByCid(cid);	
		System.out.println(consumer+"===============用户信息===========");
		List<ShopCart> slist=shopCarService.getShopCarInfoByStatus(2,cid);
		System.out.println("00000000000000000000000000");
		for (ShopCart shopCart : slist) {
			System.out.println(shopCart);
		}
		System.out.println("11111111111111111111111111");

		ShopCart sc=new ShopCart();
		Map<String,Object>map =new  HashMap<String, Object>();
		for (ShopCart shopCart : slist) {
			//map.put("count",(shopCart.getScnum()*shopCart.getScPrice()));
			//封装小计
			shopCart.setCount(shopCart.getScnum()*shopCart.getScPrice());
			//封装总金额
			sumPrice=shopCart.getCount()+sumPrice;
			System.out.println(shopCart);
		}
		
		map.put("slist",slist);
		//map.put("count", value)
		/*map.put("slist",slist );
		map.put("AddressInfo", list);
		map.put("consumserInfo", consumer);*/
		mv.addObject("map",map );
		mv.addObject("AddressInfo", list);
		mv.addObject("consumserInfo", consumer);
		mv.addObject("sum", sumPrice);
		System.out.println(consumer);		
		System.out.println("----------------------------------");
		return mv;  
	}
	@ResponseBody
	@RequestMapping("BookController")
	public String IntoOrder(HttpServletRequest request, String data ){
		 Map<String,Object> map=new HashMap<>();		
		int cid=(int) request.getSession().getAttribute("CURR_CID");
		//封装购物车信息
		Gson gson=new Gson();
		System.out.println("**********"+data);
		Map<String, List<ShopCart>> fromJson = gson.fromJson(data, new TypeToken<Map<String, List<ShopCart>>>(){}.getType());
		//System.out.println("xxxxxxxxxxxxxxxxxxxxxxxx"+fromJson);
		
		List<ShopCart> scList=fromJson.get("datas");
		System.out.println("----------------------------------");
		for (ShopCart shopCart : scList) {
			shopCart.setConsumer(new Consumer(cid));
			//int result=shopCarService.AddShopCart(shopCart);
			//System.out.println("--------"+result);
			shopCart.setSc_status(2);
	
			int num=shopCarService.updateShopCartStatus(shopCart);
			//System.out.println(num+"--------插入后的返回值---------");
			//System.out.println(shopCart);
		}
		 System.out.println("----------------------------------");
		// map.put("sclist", scList);
		// map.put("test", 1);
		//mv.addObject("scList",scList);
		//redirectAttributes.addAllAttributes(map);
		//redirect:/BookOrder
		 
		return gson.toJson(map);
	}
	public void setCakeserver(CakeServers cakeserver) {
		this.cakeserver = cakeserver;
	}

	public void setAddressServers(AddressServers addressServers) {
		this.addressServers = addressServers;
	}
	
	
}
